from apps.booking.models import AppointmentClientServices, Appointment
from apps.client.models import Client


def get_service_clients(clients_ids, clients, obj):
    for client_id in clients_ids:
        client_services = AppointmentClientServices.objects.filter(appointment=obj, client_id=client_id)

        services = []
        for client_service in client_services:
            current_service = {
                "id": str(client_service.service.id),
                "code": client_service.service.code,
                "service": client_service.service.service,
                "fee": client_service.service.fee,
                "time": client_service.service.time
            }
            service = {
                "service": current_service,
                "fees": str(client_service.fees)
            }
            services.append(service)

        client = Client.objects.get(id=client_id)
        client_data = {
            "id": str(client.id),
            "first_name": client.first_name,
            "middle_name": client.middle_name,
            "last_name": client.last_name,
            "billing_type": client.billing_type
        }
        service_client = {
            "id": client_data,
            "services": services
        }
        clients.append(service_client)

    return clients


def get_non_service_clients(client_ids, clients):
    for client_id in client_ids:
        client_instance = Client.objects.filter(id=client_id).first()
        client_data = {
            "id": {
                "id": str(client_instance.id),
                "first_name": client_instance.first_name,
                "middle_name": client_instance.middle_name,
                "last_name": client_instance.last_name,
                "billing_type": client_instance.billing_type
            }
        }
        clients.append(client_data)

    return clients


def appointment_services(obj):
    clients = []
    if AppointmentClientServices.objects.filter(appointment=obj).exists():
        clients_ids = AppointmentClientServices.objects.filter(appointment=obj).distinct('client').values_list(
            'client__id', flat=True)
        if len(clients_ids) == len(obj.clients.all()):
            get_service_clients(clients_ids, clients, obj)

        else:

            get_service_clients(clients_ids, clients, obj)
            non_service_clients = []
            client_ids = Appointment.objects.filter(id=obj.id).distinct('clients').values_list('clients__id',
                                                                                               flat=True)
            instance_uuids = set(client for client in client_ids) if client_ids else set()
            for id in instance_uuids:
                if id is not None:
                    if id not in clients_ids:
                        non_service_clients.append(id)
            if len(non_service_clients) > 0:
                get_non_service_clients(non_service_clients, clients)

    else:
        if len(obj.clients.all()) > 0:
            output = []
            for client in obj.clients.all():
                output.append({
                    'id': {
                        'id': client.id,
                        'first_name': client.first_name,
                        'middle_name': client.middle_name,
                        'last_name': client.last_name,
                        'billing_type': client.billing_type
                    },
                    'services': []
                })
            return output
        return [{
            "id": {},
            "services": []
        }]
        # client_ids = Appointment.objects.filter(id=obj.id).distinct('clients').values_list('clients__id', flat=True)
        # client_ids = [client_id for client_id in client_ids if client_id is not None]
        # if client_ids:
        #
        #     clients = []
        #     get_non_service_clients(client_ids, clients)
    return clients